<!DOCTYPE html>
<html>
<head>
<title>Physics Diagram - Rod on a Rock</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
  }
  canvas {
    border: 1px solid #999;
  }
</style>
</head>
<body>

<canvas id="physicsCanvas" width="450" height="450"></canvas>

<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // White background
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 1.5;

    // --- Helper function to draw an arrow ---
    function drawArrow(ctx, fromx, fromy, tox, toy) {
        const headlen = 8; // length of head in pixels
        const dx = tox - fromx;
        const dy = toy - fromy;
        const angle = Math.atan2(dy, dx);
        ctx.beginPath();
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }
    
    // --- Draw Rod ---
    // Define rod endpoints
    const p1 = { x: 80, y: 300 };
    const p2 = { x: 380, y: 150 };
    const thickness = 3;
    const angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
    // Calculate offsets for thickness, perpendicular to the rod's angle
    const dx_offset = thickness * Math.cos(angle + Math.PI / 2);
    const dy_offset = thickness * Math.sin(angle + Math.PI / 2);

    ctx.beginPath();
    ctx.moveTo(p1.x - dx_offset, p1.y - dy_offset);
    ctx.lineTo(p2.x - dx_offset, p2.y - dy_offset);
    ctx.lineTo(p2.x + dx_offset, p2.y + dy_offset);
    ctx.lineTo(p1.x + dx_offset, p1.y + dy_offset);
    ctx.closePath();
    ctx.stroke();

    // --- Draw Hand ---
    ctx.beginPath();
    // A simple representation of a hand gripping the rod end
    ctx.arc(p1.x - 5, p1.y + 12, 12, Math.PI * 0.8, Math.PI * 1.9);
    ctx.lineTo(p1.x + 2, p1.y);
    ctx.stroke();

    // --- Draw Rock ---
    ctx.beginPath();
    ctx.moveTo(220, 340);
    ctx.bezierCurveTo(200, 360, 210, 380, 235, 380);
    ctx.bezierCurveTo(260, 390, 285, 370, 280, 350);
    ctx.bezierCurveTo(275, 335, 250, 335, 220, 340);
    ctx.closePath();
    ctx.stroke();

    // --- Draw Rotational Arrow (ω) ---
    const arc_center = { x: 350, y: 130 };
    const arc_radius = 40;
    ctx.beginPath();
    // Clockwise arc. Start angle > end angle, and anticlockwise=false.
    // This arc goes from top-right to top-left, over the top.
    ctx.arc(arc_center.x, arc_center.y, arc_radius, Math.PI * 1.9, Math.PI * 1.1, false);
    ctx.stroke();
    
    // Arrowhead for the rotational arrow
    const arc_end_angle = Math.PI * 1.1;
    const arc_end_x = arc_center.x + arc_radius * Math.cos(arc_end_angle);
    const arc_end_y = arc_center.y + arc_radius * Math.sin(arc_end_angle);
    // The tangent angle for a clockwise arc is angle + PI/2
    const tangent_angle = arc_end_angle + Math.PI / 2;
    ctx.beginPath();
    ctx.moveTo(arc_end_x, arc_end_y);
    ctx.lineTo(arc_end_x - 8 * Math.cos(tangent_angle - Math.PI / 6), arc_end_y - 8 * Math.sin(tangent_angle - Math.PI / 6));
    ctx.moveTo(arc_end_x, arc_end_y);
    ctx.lineTo(arc_end_x - 8 * Math.cos(tangent_angle + Math.PI / 6), arc_end_y - 8 * Math.sin(tangent_angle + Math.PI / 6));
    ctx.stroke();

    // --- Draw Linear Velocity Arrows ---
    // Define start points on the rod's centerline
    const arrow1_start = { x: 200, y: 240 };
    const arrow2_start = { x: 305, y: 187.5 };
    // The velocity vector is perpendicular to the rod's vector
    const arrow_vec = { x: p1.y - p2.y, y: p2.x - p1.x }; 
    const arrow_len = 30;
    const norm = Math.sqrt(arrow_vec.x**2 + arrow_vec.y**2);
    
    const arrow1_end = {
        x: arrow1_start.x + (arrow_vec.x / norm) * arrow_len,
        y: arrow1_start.y + (arrow_vec.y / norm) * arrow_len
    };
    drawArrow(ctx, arrow1_start.x, arrow1_start.y, arrow1_end.x, arrow1_end.y);

    const arrow2_end = {
        x: arrow2_start.x + (arrow_vec.x / norm) * arrow_len,
        y: arrow2_start.y + (arrow_vec.y / norm) * arrow_len
    };
    drawArrow(ctx, arrow2_start.x, arrow2_start.y, arrow2_end.x, arrow2_end.y);

    // --- Draw Labels ---
    ctx.font = 'italic 20px serif';
    ctx.fillText('O', p1.x - 25, p1.y + 20);
    ctx.fillText('ω', arc_center.x, arc_center.y - arc_radius - 10);
    
    ctx.font = '16px sans-serif';
    ctx.fillText('rock', 290, 370);
    ctx.fillText('Fig. 1.125.', 180, 430);

</script>

</body>
</html>